import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/user/Users.vue'
import Roles from '../components/power/Roles.vue'
import Rights from '../components/power/Rights.vue'
import Box from '../components/myComponents/Box'
import Cate from '../components/Goods/Cate.vue'
import Params from '../components/Goods/Params.vue'
import Goodslist from '../components/Goods/Goodslist.vue'
import Add from '../components/Goods/Add.vue'
Vue.use(VueRouter)
Vue.component('box', Box)

const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    // 主页页面路由
    {
      path: '/home',
      component: Home,
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/users', component: Users },
        { path: '/roles', component: Roles },
        { path: '/rights', component: Rights },
        { path: '/categories', component: Cate },
        { path: '/params', component: Params },
        { path: '/goods', component: Goodslist },
        { path: '/goods/add', component: Add },
      ],
      redirect: '/welcome'
    }
  ]
})
router.beforeEach((to, from, next) => {
  // to.path 是将要访问得路径
  // from.path 是从哪里来得路径
  // next 放行
  // 登陆页面是任何人都可以访问的
  if (to.path == '/login') return next();
  // 判断是否有token来决定是否放行
  const isToken = window.sessionStorage.getItem('token');
  // next里面加地址表示跳转到什么地方
  if (!isToken) return next('/login');
  next()
})
export default router
